package com.chat.datea.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chat.datea.R;
import com.chat.datea.bean.FindEntity;
import com.chat.datea.utils.cache.MyConfig;

import java.util.List;

/**
 *
 * music发现页面适配器
 * Created by dllo on 15/10/23.
 */
public class FindFragment_ListView_Adapter extends BaseAdapter {
    private List<FindEntity.ResultEntity.ItemsEntity> data;
    private FindEntity findEntity;
    private Context context;

    public FindFragment_ListView_Adapter(Context c, FindEntity entity) {
        context = c;
        findEntity = entity;
        data = findEntity.getResult().getItems();

    }

    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return data != null && data.size() > 0 ? data.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            //通过布局加载器来加载行布局
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_find_listview_item, null);
            //初始化控件
            holder.find_Name = (TextView) convertView.findViewById(R.id.find_Name);
            holder.find_Person = (TextView) convertView.findViewById(R.id.find_Person);
            holder.find_Hot = (TextView) convertView.findViewById(R.id.find_Hot);
            holder.find_Btn = (ImageView) convertView.findViewById(R.id.find_Btn);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        final FindEntity.ResultEntity.ItemsEntity itemsEntity = (FindEntity.ResultEntity.ItemsEntity) getItem(position);
        //设置组件
        holder.find_Name.setText(data.get(position).getSong_title());
        holder.find_Person.setText(data.get(position).getArtist_name());
        holder.find_Hot.setText(data.get(position).getPlay_num());
        String song_id =itemsEntity.getSong_id();
        String song_title = itemsEntity.getSong_title();
        MyConfig.putSharePre(context, "song_id", "song_id", song_id);
        MyConfig.putSharePre(context, "album_title", "album_title",song_title);
        return convertView;
    }

    public class ViewHolder {
        private TextView find_Name, find_Person, find_Hot;
        private ImageView find_Btn;
    }
}
